<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\index\controller;
use think\App;
use app\index\model\Template as TemplateModel;
use app\index\model\News as NewsModel;
use app\index\model\Email as EmailModel;
use app\index\model\Content as ContentModel;
/**
 * 前台首页控制器
 * @package app\index\controller
 */
class Index extends Home
{
    public function __construct(App $app = null)
    {
        parent::__construct($app);
    }



    public function products(){
        $tag_num=config('setting.tab_number');
        $language=$this->language;
        $fields=[];
        for ($i=1;$i<=$tag_num;$i++){
            $fields[]='image_'.$i;
            $fields[]='title_'.$language.'_'.$i;
        }
        $fields[]='create_time';
        $TemplateModel=new TemplateModel();
        $list=$TemplateModel->getList($fields);
        //get_file_path();
        return $this->fetch();
    }

    public function news(){
        $NewsModel=new NewsModel();
        $list=$NewsModel->getLists();
        return $this->fetch('news',['data'=>$list]);
    }


    public function news_detail($id){
        if(empty($id)) $this->error('页面不存在');
        $NewsModel=new NewsModel();
        $info=$NewsModel->getList($id);
        $next_info=NewsModel::field((['id']))->order('id','asc')->where('id','>=',$id)->limit(2)->select()->toArray();
        $info['next_id']=0;
        $info['pre_id']=0;
        if(isset($next_info[1])){
            $info['next_id']=$next_info[1];
        }
        $pre_info=NewsModel::field((['id']))->order('id','desc')->where('id','<=',$id)->limit(2)->select()->toArray();
        if(isset($pre_info[1])){
            $info['pre_id']=$pre_info[1];
        }
        if(empty($info)) $this->error('页面不存在');
        $this->assign('data',$info);
        return $this->fetch();
    }

    public function news_more($page){
        $NewsModel=new NewsModel();
        $list=$NewsModel->getLists($page);
        return $this->fetch('news_more',['data'=>$list]);
    }

    /**
     * 关于我们
     * @return mixed
     */
    public function about_us(){
        return $this->fetch();
    }

    /**
     * 娱乐
     * @return mixed
     */
    public function entertainment(){
        $NewsModel=new NewsModel();
        $list=$NewsModel->getAllLists();
        // 默认跳转模块
        $this->assign('data',$list);
        return $this->fetch();
    }

    /**
     * 平台
     */
    public function platform(){
        return $this->fetch();
    }

    public function innovation(){
        $NewsModel=new NewsModel();
        $list=$NewsModel->getAllLists();
        // 默认跳转模块
        $this->assign('data',$list);
        return $this->fetch();
    }

    public function service(){
        return $this->fetch();
    }

    public function contact_us(){
        if($this->request->method()=='POST'){
            $bool=false;
            $data['email']=$this->request->param('email');
            $data['user_name']=$this->request->param('user_name');
            $data['phone']=$this->request->param('phone');
            $data['company']=$this->request->param('company');
            $data['content']=$this->request->param('content');
            if(empty($data['email'])
                || empty($data['user_name'])
                || empty($data['phone'])
                || empty($data['company'])
                || empty($data['content'])) {
                return $this->error('Incomplete parameters');
            }
            $data['create_time']=date('Y-m-d H:i:s');
            $ContentModel = new ContentModel();
            if($ContentModel->save($data)){
                $bool=send_email($data['user_name'],'官网用户--联系我们',$data['content'],config('setting.receive_replay_email'));
            }
            if($bool){
                return $this->success('send success');
            }else{
                return $this->error('send failed');
            }
        }else{
            return $this->fetch();
        }
    }

    public function join_us(){
        $email=$this->request->param('email');
        if(empty($email)) $this->success('Email cannot be empty!');
        $id=EmailModel::insert(['email'=>$email,'status'=>1,'create_time'=>date('Y-m-d H:i:s')]);
        if($id){
            return $this->success('send success');
        }else {
            return $this->error('send failed');
        }
    }
}
